
#include "device/ip_scan.h"


namespace iecas_ns
{
    shared_ptr<CIpScan> CIpScan::sm_intancePtr = nullptr;

    CIpScan::CIpScan()
    {
        m_commonPtr = shared_ptr<CCommon>(CCommon::getInstance());
        m_jsonUsrPtr = shared_ptr<CJsonUsr>(CJsonUsr::getInstance());
        m_devCommonPtr = shared_ptr<CDevCommon>(CDevCommon::getInstance());
        pwd_shell = "sh $PWD/";
        ip_current.clear();
        ip_dev_name.clear();
    }

	shared_ptr<CIpScan> CIpScan::getInstance()
	{
		if(sm_intancePtr == nullptr)
		{
			sm_intancePtr = shared_ptr<CIpScan>(new CIpScan());
		}
		return sm_intancePtr;
	}

    bool CIpScan::init(void)
    {
        return true;
    }

    void CIpScan::start(void)
    {
        thread tempThread(&CIpScan::loop, this);
        tempThread.detach();
    }

    void CIpScan::loop(void)
    {
        string cmd_shell;
        int64_t loop_time,start_time,end_time;
        cmd_shell = pwd_shell + INIT_IP_SCAN + " " + CONNECTED_IP_TXT_FILE_NAME;
        
        while(true)
        {
            start_time = m_commonPtr->getMsTimeStamp();
            system(cmd_shell.c_str());
            get_ip_not_socket();
            ip_dev_up_down();
            get_ip_dev_name(ip_dev_name);
            ip_dev_name.clear();
            end_time = m_commonPtr->getMsTimeStamp();
            loop_time = end_time - start_time;
            usleep(5000000-loop_time*1000);
        }
    }

    void CIpScan::get_ip_not_socket(void)
    {
        ifstream ifs;

        ifs.open(CONNECTED_IP_TXT_FILE_NAME, ios::in);
        if (!ifs.is_open()) 
        {
            cout << "open " << CONNECTED_IP_TXT_FILE_NAME << " failed" << endl;
            PLOGD << "open " << CONNECTED_IP_TXT_FILE_NAME << " failed";
            return;
        }

        ip_current.clear();

        string tmp;
        while(getline(ifs, tmp))
        {
            cout << "get net dev: " << tmp << endl;
            PLOGD << "get net dev: " << tmp;
            ip_dev_name.push_back(tmp);
            if(!m_commonPtr->isIpSocket(tmp))
            {
                ip_current.push_back(tmp);
            }
        }
        ifs.close();
    }

    void CIpScan::ip_dev_up_down(void)
    {
        int i, j;
        vector<string> ip_last;

        m_commonPtr->getIpNotSocket(ip_last);

        for(i = 0; i < ip_current.size(); i++)
	    {
            for(j = 0; j < ip_last.size(); j++)
            {
                if(ip_last[j] == ip_current[i])
                {
                    break;
                }
                else
                {
                    continue;
                }
            }
            if(j == ip_last.size())
            {
                cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!dev up!!!!!:" << ip_current[i] << endl;
                PLOGD << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! dev up" << ip_current[i];
                m_commonPtr->addDev(ip_current[i]);
            }
        }

        for(i = 0; i < ip_last.size(); i++)
	    {
            for(j = 0; j < ip_current.size(); j++)
            {
                if(ip_current[j] == ip_last[i])
                {
                    break;
                }
                else
                {
                    continue;
                }
            }
            if(j == ip_current.size())
            {
                cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!dev down!!!!!:" << ip_last[i] << endl;
                PLOGD << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! dev down" << ip_last[i];
                m_commonPtr->delDev(ip_last[i]);
            }
        }
    }

    void CIpScan::get_ip_dev_name(vector<string> ip_dev_name)
    {
		int res,i = 0;
        string tmp_name;

        m_commonPtr->delIpName();
        ifstream ifs;
        ifs.open(DHCP_LEASE_TXT_FILE_NAME, ios::in);
        if (!ifs.is_open()) 
        {
            cout << "open " << DHCP_LEASE_TXT_FILE_NAME << " failed" << endl;
        }

        string tmp;
        while(getline(ifs, tmp))
        {
            for(i = 0;i < ip_dev_name.size();i++)
            {
                res = tmp.find(ip_dev_name[i]);
			    if(res != -1 && ((res + ip_dev_name[i].size()) < tmp.size()))
			    {
                    tmp_name = tmp.substr(res+ip_dev_name[i].size()+1, (tmp.size()-res-ip_dev_name[i].size()-21));
                    m_commonPtr->addIpName(ip_dev_name[i] , tmp_name);
                    break;
                }
                else
                {
                    continue;
                }
            }
        }
        ifs.close();
    }
}